package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.common.ResponseResult;
import com.shop.entity.*;
import com.shop.entity.vo.ManageRoleVo;
import com.shop.entity.vo.RightsVo;
import com.shop.mapper.GoodsMapper;
import com.shop.mapper.OederGoodsMapper;
import com.shop.mapper.OrdersMapper;
import com.shop.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OederGoodsMapper oederGoodsMapper;

    @Override
    public List<Orders> selectAll() {
        List<Orders> list = ordersMapper.selectList(null);
        return list;
    }


    @Override
    public ResponseResult selectPage(@RequestParam(required = false) Integer pagesize,
                                     @RequestParam(required = false) Integer pagenum,
                                     @RequestParam(required = false) String query) {
        ResponseResult result = new ResponseResult();
        Page<Orders> page = new Page<>(pagesize, pagenum);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.like("order_id",query);
        page = ordersMapper.selectPage(page, wrapper);
        result.setSuccess("查询成功",page);
        return result;
    }

    @Override
    public ResponseResult selectVo( Integer pagesize, Integer pagenum, String query) {
        ResponseResult result = new ResponseResult();
        Page<Orders> page = new Page<>(pagesize,pagenum);
        if (query==null){
            query="";
        }
        QueryWrapper<Orders> wrapper = new QueryWrapper();
        wrapper.like("order_id",query);
        page = ordersMapper.selectPage(page,wrapper);
        result.setSuccess("查询成功",page);
        return result;
    }

//    @Override
//    public ResponseResult addOrders(Orders orders) throws Exception {
//        ResponseResult result = new ResponseResult<>();
//        long nowTime = System.currentTimeMillis()/1000;
//        Integer nowTimeInt = new Long(nowTime).intValue();
//        orders.setCreateTime(nowTimeInt);
//        orders.setUpdateTime(nowTimeInt);
//        int insert = ordersMapper.insert(orders);
//        if(insert !=1){
//            result.setCREATED("添加失败",orders);
//            throw new Exception("添加失败");
//        }
//        else {
//            result.setCREATED("添加成功",orders);
//        }
//        return result;
//    }

    @Override
    public ResponseResult updateOrders(Integer id, Orders orders) throws Exception {
        ResponseResult result=new ResponseResult();
        long nowTime = System.currentTimeMillis()/1000;
        Integer nowTimeInt = new Long(nowTime).intValue();
        orders.setUpdateTime(nowTimeInt);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",id);
        int insert = ordersMapper.update(orders,wrapper);
        if(insert !=1){
            result.setSuccess("修改失败",orders);
            throw new Exception("修改失败");
        }
        else {
            orders.setGoods(selectGood(orders.getOrder_id()));
            result.setSuccess("修改成功",orders);
        }
        return result;
    }

    @Override
    public ResponseResult deleteOrders(Integer id) throws Exception {
        ResponseResult result = new ResponseResult();
        int insert=ordersMapper.deleteById(id);
        if(insert !=1){
            result.setDELETED("删除失败");
            throw new Exception("修改失败");
        }
        else {
            result.setDELETED("删除成功");
        }
        return result;
    }

//    @Override
//    public ResponseResult selectOne(Integer id) {
//        ResponseResult result=new ResponseResult();
//        Orders orders=ordersMapper.selectById(id);
//        if(orders==null){
//            result.setSuccess("没有查询到相关内容");
//        }else {
//            result.setSuccess("查询成功",orders);
//        }
//        return result;
//    }

    @Override
    public ResponseResult selectOne(Integer id) {
        ResponseResult result = new ResponseResult();
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.eq("order_id",id);
        Orders orders = ordersMapper.selectOne(ordersQueryWrapper);
        orders.setGoods(selectGood(orders.getOrder_id()));
        result.setSuccess("查询成功",orders);
        return result;
    }
    private List<OrderGoods> selectGood(Integer id){
        QueryWrapper<OrderGoods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.eq("order_id",id);
        List<OrderGoods> list = oederGoodsMapper.selectList(goodsQueryWrapper);
        return list;
    }

//    @Override
//    public ResponseResult updateOne(Integer id, Orders orders) {
//        ResponseResult result = new ResponseResult();
//        orders.setOrderId(id);
//        int insert = ordersMapper.updateById(orders);
//        if (insert==1){
//            result.setCREATED("修改成功",orders);
//        }else {
//            result.setINTERNALSERVERERROR();
//        }
//        return result;
//    }


//    @Override
//    public ResponseResult selectGoods(Integer id) {
//        ResponseResult result = new ResponseResult();
//        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.eq("order_id",id);
//      List<OrdersVo> list= ordersMapper.selectList(wrapper);
//        System.out.println(list);
////      for(OrdersVo ordersVo:list){
////          ordersVo.setOrderId(id);
////      }
//
//        return result;
//    }


}
